package akshay.drawing;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class EditActivity extends Activity implements OnClickListener{
static Button Type,bgcolor,strokecolor,textcolor,done;
EditText txt_username;
SeekBar sb;
private static int circleID=0;
CharSequence[] items = { "Circle", "Rectangle", "Trigangle" };
CharSequence[] colors = { "Yellow", "Green", "Red" };
String namechanged="";
static Display myview;
Circle c;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		int count=0;
		setContentView(R.layout.edit);
		 myview=new Display(getApplicationContext());
		 Bundle bId=getIntent().getExtras();
		 if(bId!=null)
		 {
			 circleID=Integer.parseInt((bId.getString("ID").toString()));
		 }
		 
		 Log.d("ID=", ""+circleID);
		
		 Log.d("", "wooooooo");
		 txt_username =(EditText) findViewById(R.id.txtLab);
		 
		Type=(Button)findViewById(R.id.btType);
		
		bgcolor=(Button)findViewById(R.id.btBgColor);
		
		strokecolor=(Button)findViewById(R.id.btnStColor);
		
		textcolor=(Button)findViewById(R.id.btTxtColor);
		done=(Button)findViewById(R.id.btDone);
		sb=(SeekBar)findViewById(R.id.sbStrock);
		Log.d("cir", ""+myview.c.getShape());
		Type.setText(myview.c.getShape());
		Type.setOnClickListener(this);
		bgcolor.setOnClickListener(this);
		strokecolor.setOnClickListener(this);
		textcolor.setOnClickListener(this);
		done.setOnClickListener(this);
		while(Display.al.size()>count){
			if(Display.al.get(count).getId()==circleID){
				Log.d("size", ""+Display.al.get(count).getCircle().x);
				c=Display.al.get(count);
				count++;
			}
			else {
				Log.d("else size", ""+Display.al.get(count).getCircle().x);
				count++;
			}
		}
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(((Button)v).equals(Type)){
			showDialog(0);
		}
		else if(((Button)v).equals(bgcolor)){
			showDialog(1);
		}
		else if(((Button)v).equals(strokecolor)){	
		}
		else if(((Button)v).equals(textcolor)){	
			showDialog(3);
		}
		else if(((Button)v).equals(done)){
			myview.c.p.setStrokeWidth(sb.getProgress()/10);
			 myview.invalidate();
   		  myview.setVisibility(0);
   		Intent newintent = new Intent();
        newintent.setClass(getApplicationContext(),SmartDrawingActivity.class);
        Log.d("activity", "before act");
        newintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(newintent);
		}
	}
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 0:
		return new AlertDialog.Builder(this)
		.setPositiveButton("OK", new
				DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
				int whichButton)
				{
				Toast.makeText(getBaseContext(),
				"OK clicked!", Toast.LENGTH_SHORT).show();
				}
				})
				.setNegativeButton("Cancel", new
				DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
				int whichButton)
				{
				Toast.makeText(getBaseContext(),
				"Cancel clicked!", Toast.LENGTH_SHORT).show();
				}
				}).setSingleChoiceItems(items, items.length, new
				DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						if(which==0){
							namechanged=items[0].toString();
							Type.setText(namechanged);
						}
						else if(which==1){
						namechanged=items[1].toString();
						Type.setText(namechanged);
						}
						else if(which==1){
							namechanged=items[2].toString();
							Type.setText(namechanged);
							}
					}
				})
			.create();
		case 1:
			return new AlertDialog.Builder(this)
			.setPositiveButton("OK", new
					DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
					int whichButton)
					{
					Toast.makeText(getBaseContext(),
					"OK clicked!", Toast.LENGTH_SHORT).show();
					}
					})
					.setNegativeButton("Cancel", new
					DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
					int whichButton)
					{
					Toast.makeText(getBaseContext(),
					"Cancel clicked!", Toast.LENGTH_SHORT).show();
					}
					}).setSingleChoiceItems(colors,colors.length, new
					DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							if(which==0){
								c.p.setColor(Color.YELLOW);
								bgcolor.setBackgroundColor(Color.YELLOW);
							}
							else if(which==1){
								c.p.setColor(Color.GREEN);
								bgcolor.setBackgroundColor(Color.GREEN);
							}
							else if(which==2){
								c.p.setColor(Color.RED);
								bgcolor.setBackgroundColor(Color.RED);
								}
						}
					})
				.create();
		case 2:
			return new AlertDialog.Builder(this)
			.setPositiveButton("OK", new
					DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
					int whichButton)
					{
					
					}
					})
					.setNegativeButton("Cancel", new
					DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
					int whichButton)
					{
					Toast.makeText(getBaseContext(),
					"Cancel clicked!", Toast.LENGTH_SHORT).show();
					}
					}).setSingleChoiceItems(colors,colors.length, new
					DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							if(which==0){
								//bgcolor.setBackgroundColor(Color.YELLOW);
							}
							else if(which==1){
								//bgcolor.setBackgroundColor(Color.GREEN);
							}
							else if(which==2){
								//bgcolor.setBackgroundColor(Color.RED);
								}
						}
					})
				.create();
		case 3:
			return new AlertDialog.Builder(this)
			.setPositiveButton("OK", new
					DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
					int whichButton)
					{
					Toast.makeText(getBaseContext(),
					"OK clicked!", Toast.LENGTH_SHORT).show();
					}
					})
					.setNegativeButton("Cancel", new
					DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
					int whichButton)
					{
					Toast.makeText(getBaseContext(),
					"Cancel clicked!", Toast.LENGTH_SHORT).show();
					}
					}).setSingleChoiceItems(colors,colors.length, new
					DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							if(which==0){
								CharSequence Text=txt_username.getText();
								txt_username.setTextColor(Color.YELLOW);
								txt_username.setText(Text);
								textcolor.setBackgroundColor(Color.YELLOW);
								c.setText(txt_username.getText().toString());
							}
							else if(which==1){
								CharSequence Text=txt_username.getText();
								txt_username.setTextColor(Color.GREEN);
								txt_username.setText(Text);
								textcolor.setBackgroundColor(Color.GREEN);
								c.setText(txt_username.getText().toString());
							}
							else if(which==2){
								CharSequence Text=txt_username.getText();
								txt_username.setTextColor(Color.RED);
								txt_username.setText(Text);
								textcolor.setBackgroundColor(Color.RED);
								c.setText(txt_username.getText().toString());
								}
						}
					})
				.create();
				}
			
			return null;
			}
	public static Circle currentCircle(){ 
		int count=0;
		while(Display.al.size()>count){
			if(Display.al.get(count).getId()==circleID){
				Log.d("size", ""+Display.al.get(count).getCircle().x);
				count++;
				return (Circle)Display.al.get(count);
			}
			else {
				Log.d("else size", ""+Display.al.get(count).getCircle().x);
				count++;
			}
		}
		Log.d("null size", ""+Display.al.get(count).getCircle().x);
		return null;
	}

}